package com.huayi.server.Controller.app;


import com.huayi.server.Entity.DeviceEntity;
import com.huayi.server.Forms.CtlForm;
import com.huayi.server.NioServer.DeviceManager;
import com.huayi.server.Utils.Resp;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by mac on 2017/8/22.
 */
@Controller
@RequestMapping("/control")
public class ControlController {

    Logger logger = LoggerFactory.getLogger(ControlController.class);

    @Autowired
    Resp resp;

    @Autowired
    DeviceManager deviceManager;

    /**
     * @deprecated 单个也从多个控制 fk
     * 控制单个
     * {id:1234,deviceEntity:{"id": "0004","open": true}}
     * @param form
     * @return
     */
    @RequestMapping(value = "/ctl",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Resp control(@RequestBody CtlForm form){
        deviceManager.setId(form.getId());
        deviceManager.multiCtl(Arrays.asList(form.getDeviceEntity()));//错误
        return resp;
    }

    /**
     * 批量控制
     * 1 控制开关
     {
         "id": 1234,
         "type": 1,
         "deviceList": [
             {
             "deviceId": "0001",
             "open": true,
             "time": 300
             },{
             "deviceId": "0002",
             "open": true,
             "time": 300
             },{
             "deviceId": "0003",
             "open": true,
             "time": 300
             },{
             "deviceId": "0004",
             "open": true,
             "time": 300
             }]
     }
     -------------------------------
     * 3 改变配置
     {
        "id": 1234,
        "type": 3,
        "deviceList": [
             {"deviceId": "0001",
             "at": 33,
             "autoTmpFlag": true,
             "ah": 88,
             "autoHumFlag": true
             },{
             "deviceId": "0002",
             "at": 33,
             "autoTmpFlag": true,
             "ah": 88,
             "autoHumFlag": true
             },{
             "deviceId": "0003",
             "at": 33,
             "autoTmpFlag": true,
             "ah": 88,
             "autoHumFlag": true
             },{
             "deviceId": "0004",
             "at": 33,
             "autoTmpFlag": true,
             "ah": 88,
             "autoHumFlag": true
             }]
     }
     *
     * @param form
     * @return
     */
    @RequestMapping(value = "/multicontrol",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public Resp multicontrol(@RequestBody CtlForm form){
        deviceManager.setId(form.getId());
        deviceManager.setType(form.getType());
        List<DeviceEntity> list = form.getDeviceList();
        //需要干掉不在线的设备  APP界面应该让不在线的设备无法调整
//        for (DeviceEntity deviceEntity : list){
//            if (!deviceEntity.getOnline()){
//                list.remove(deviceEntity);
//            }
//        }
        Future<Boolean> sendMessageFuture = deviceManager.multiCtl(list);
        try {
            if (sendMessageFuture.await(3, TimeUnit.SECONDS) && sendMessageFuture.isSuccess()){
                resp.setCode(200);
                resp.setDesc("发送成功....");
            }else{
                resp.setCode(199);
                resp.setDesc(sendMessageFuture.cause().getMessage());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            resp.setCode(198);
        }
        return resp;
    }

}
